﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Entity;
using Entity;
using System.Data.SqlClient;
using System.Data;
using System.Data.Sql;
using System.Data.SqlTypes;

namespace DAL
{
    public class ContatoWrapper : BaseWrapper, IWrapper<Contato>
    {
        public List<Contato> GetAll()
        {
            throw new NotImplementedException();
        }

        public Contato GetById(int id)
        {
            throw new NotImplementedException();
        }

        public bool Insert(Contato entity)
        {
            bool status = false;
            SqlConnection cn = new SqlConnection(ConnectionString());
            SqlCommand cmd = new SqlCommand("SP_CONTATO_INSERT", cn);
            cmd.CommandType = CommandType.StoredProcedure;
            SqlParameter parameter = new SqlParameter("@IDCONTATO", DbType.Int32);
            parameter.Direction = ParameterDirection.Output;
            cmd.Parameters.Add(parameter);

            cmd.Parameters.AddWithValue("@IDUSUARIO", entity.IDUSUARIO);
            cmd.Parameters.AddWithValue("@DDD", entity.DDD);
            cmd.Parameters.AddWithValue("@TELEFONE", entity.TELEFONE);

            if (string.IsNullOrEmpty(entity.RAMAL))
                cmd.Parameters.AddWithValue("@RAMAL", "");
            else
                cmd.Parameters.AddWithValue("@RAMAL", entity.RAMAL);

            try
            {
                cn.Open();
                if (cmd.ExecuteNonQuery() > 0)
                    status = true;

            }
            catch (SqlException ex)
            {
                Messages += "Erro na inserção de dados\n";
                Messages += ex.Message.ToString();
                Logs.MENSAGEM = Messages;
                Logs.TIPOLOG = "Manipulação de dados";
                LOGsWrapper LogsWrapper = new LOGsWrapper();
                LogsWrapper.Insert(Logs);

            }
            finally
            {
                if (cn.State != ConnectionState.Closed)
                    cn.Close();
            }

            return status;

        }

        public bool Update(Contato entity)
        {
            bool status = false;
            SqlConnection cn = new SqlConnection(ConnectionString());
            SqlCommand cmd = new SqlCommand("SP_CONTATO_UPDATE", cn);
            cmd.CommandType = CommandType.StoredProcedure;

            SqlParameter parametro = new SqlParameter("@IDCONTATO", DbType.UInt32);
            parametro.Direction = ParameterDirection.Output;

            cmd.Parameters.Add(parametro);
            entity.IDCONTATO = int.Parse(cmd.Parameters[0].Value.ToString());


            cmd.Parameters.AddWithValue("IDUSUARIO", entity.IDUSUARIO);
            cmd.Parameters.AddWithValue("DDD", entity.DDD);
            cmd.Parameters.AddWithValue("TELEFONE", entity.TELEFONE);
            cmd.Parameters.AddWithValue("RAMAL", entity.RAMAL);

            try
            {
                cn.Open();
                if (cmd.ExecuteNonQuery() > 0)
                    status = true;

            }
            catch (SqlException ex)
            {
                Messages += "Erro na inserção de dados\n";
                Messages += ex.Message.ToString();
                Logs.MENSAGEM = Messages;
                Logs.TIPOLOG = "Manipulação de dados";
                LOGsWrapper LogsWrapper = new LOGsWrapper();
                LogsWrapper.Insert(Logs);
            }
            finally
            {
                if (cn.State != ConnectionState.Closed)
                    cn.Close();
            }

            return status;
        }

        public bool Delete(Contato entity)
        {
            bool status = false;
            SqlConnection cn = new SqlConnection(ConnectionString());
            SqlCommand cmd = new SqlCommand("SP_CONTATO_UPDATE", cn);
            cmd.CommandType = CommandType.StoredProcedure;
            cmd.Parameters.AddWithValue("IDCONTATO", entity.IDCONTATO);

            try
            {
                cn.Open();
                if (cmd.ExecuteNonQuery() > 0)
                    status = true;

            }
            catch (SqlException ex)
            {
                Messages += "Erro na exclusão de dados\n";
                Messages += ex.Message.ToString();
                Logs.MENSAGEM = Messages;
                Logs.TIPOLOG = "Manipulação de dados";
                LOGsWrapper LogsWrapper = new LOGsWrapper();
                LogsWrapper.Insert(Logs);

            }
            finally
            {
                if (cn.State != ConnectionState.Closed)
                    cn.Close();
            }

            return status;

        }

        public List<Contato> GetGrid(int p)
        {
            List<Contato> LISTA = new List<Contato>();

            SqlConnection cn = new SqlConnection(ConnectionString());
            SqlCommand cmd = new SqlCommand("SP_CONTATO_GETBYIDUSUARIO", cn);
            cmd.CommandType = CommandType.StoredProcedure;
            cmd.Parameters.AddWithValue("@IDUSUARIO", p);

            ////Parametros
            //// cmd.Parameters.AddWithValue("@NomeCampo", value)


            try
            {
                cn.Open();
                SqlDataReader dr = cmd.ExecuteReader();

                while (dr.Read())
                {
                    Contato contato = new Contato();
                    contato.IDCONTATO = int.Parse(dr["IDCONTATO"].ToString());
                    contato.DDD = dr["DDD"].ToString();
                    contato.TELEFONE = dr["TELEFONE"].ToString();
                    contato.RAMAL = dr["RAMAL"].ToString();
                    LISTA.Add(contato);
                }
            }
            catch (SqlException ex)
            {
                Messages += "Erro de execução.";
                Messages += ex.Message;
                Logs.MENSAGEM = Messages;
                Logs.TIPOLOG = "Leitura de dados";
                LOGsWrapper LogsWrapper = new LOGsWrapper();
                LogsWrapper.Insert(Logs);

            }
            finally
            {
                cn.Close();
            }

            return LISTA;

        }
    }
}
